﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Extensions.Configuration;

namespace ESoft.Helpers
{
    public class AppSetting
    {

        static IConfiguration Configuration { get; set; }

        /*
        /// <summary>
        /// 静态初始化
        /// </summary>
        static AppSetting() {

            string json = @"appsettings.json";
            Configuration = new ConfigurationBuilder()
                .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                .Add(new JsonConfigurationSource { Path = json, Optional = false, ReloadOnChange = true }).Build();
        }
        */

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration"></param>
        public AppSetting(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
        /// 封装要操作的字符
        /// </summary>
        /// <param name="sections">节点配置</param>
        /// <returns></returns>
        public static string GetString(params string[] sections)
        {
            try
            {
                if (sections.Any())
                {
                    return Configuration[string.Join(":", sections)];
                }
            }
            catch (Exception) { }

            return "";
        }

        //public static T GetConfig<T>(params string[] sections) where T : class, new()
        //{
        //    try
        //    {
        //        var instance = new T();
        //        Configuration.Bind(string.Join(":", sections), instance);
        //        return instance;
        //    }
        //    catch
        //    {
        //        return default;
        //    }
        //}
    }
}
